home *** CD-ROM | disk | FTP | other *** search
- set output error
- set display page 23
- set more 1
- #delimit ;
-
- di _n(5) in wh
- " ___ ____ ____ ____ ____ tm" _n
- " /__ / ____/ / ____/" _n
- "___/ / /___/ / /___/ How to Make Graphs" _n
- "--------------------------------------------------" _n(2) ;
-
- di in gr
- "We are about to demonstrate Stata's graphics. Actually, we will only scratch"
- _n
- "the surface of those capabilities, so if you own the professional version of"
- _n
- "Stata, you should read the Graphics chapter of the Reference Manual. If you"
- _n
- "have the Demonstration Version, after this tutorial you should feel free to" _n
- "experiment. You can learn more about the "
- in wh "graph" in gr " command by typing "
- "'" in wh "help graph" in gr "'." _n ;
-
-
- di in gr
- "Some more words of advice: If your computer and monitor do not have graphics"
- _n
- "capabilities, there is no reason to run this tutorial. You can still use" _n
- "Stata's graphics, but you will have to print them in order to see them. This"
- _n
- "tutorial assumes you will be able to see the graphs as we draw them. You can"
- _n
- "abort this tutorial by pressing "
- in wh "Break" in gr " (DOS users hold down "
- in wh "Ctrl" in gr " and press" _n
- in wh "Break" in gr ", Unix users hold down "
- in wh "Ctrl" in gr " and press " in wh "C"
- in gr "). On the next screen, we will" _n
- "verify that Stata thinks your computer has graphics capabilities." _n ;
-
- di in gr
- "If you are using a Sun under SunView, please "
- in wh "Break" in gr" out of this tutorial, type"
- _n
- "'" in wh "window define tut"
- in gr "', and then restart this tutorial. You don't have to do" _n
- "that, but results will be better if you do." _n;
-
- #delimit cr
- mac def path
- capture run nullfile.tut
- if _rc {
- mac def path "\stata\"
- capture run %path`nullfile.tut
- if _rc {
- mac def path "/usr/stata/"
- capture run %path`nullfile.tut
- if _rc {
- #delimit ;
- di in red
- "I cannot find the other tutorial files. I have looked in the current" _n
- "directory and in \stata (DOS) or /usr/stata (Unix). Is Stata installed" _n
- "correctly?" _n(2)
- "In any case, I cannot run the tutorial." ;
- #delimit cr
- exit
- }
- }
- }
-
- macro define F5 "do %path`contents.tut;"
- macro define F6 "do %path`tables.tut;"
-
- if "%path"=="/usr/stata/" {
- mac def _pause "more"
- }
-
- #delimit ;
-
-
-
- set more 0 ; more ; set more 1 ;
-
- set textsize 100 ;
- di _n(4) in wh
- "More preliminary" _n
- "----------------" _n(2)
- ". query" ;
- noisily query ;
-
- di in gr _n
- "If the above display indicates that "
- in wh "graphics" in gr " are " in wh "off"
- in gr ", Stata does not think" _n
- "your computer and monitor have graphics capabilities, so you may as well" _n
- in wh "Break" in gr " out of this tutorial." _n ;
-
- set more 0 ; more ; set more 1;
- macro define F6 "do %path`graphics.tut;" ;
-
-
- di _n(9) in wh
- "Graphic styles" _n
- "--------------" _n ;
-
- di in gr
- "Stata provides eight graphic styles, known as:" _n(2)
- _col(10) in wh "twoway" in gr _col(22) "meaning two-way scatterplots" _n
- _col(10) in wh "matrix" in gr _col(22) "meaning two-way scatterplot matrices"
- _n
- _col(10) in wh "histogram" in gr _col(22) "meaning histograms" _n
- _col(10) in wh "oneway" in gr _col(22) "meaning one-way scatterplots" _n
- _col(10) in wh "box" in gr _col(22) "meaning box-and-whisker plots" _n
- _col(10) in wh "star" in gr _col(22) "meaning star charts" _n
- _col(10) in wh "bar" in gr _col(22) "meaning bar charts" _n
- _col(10) in wh "pie" in gr _col(22) "meaning pie charts" _n ;
-
- #delimit cr
-
- capture program drop _yesno
- program define _yesno
- macro define answ
- while lower("%answ")!="y" & lower("%answ")!="n" {
- di in gr "%_1? (" in wh "y" in gr "/" in wh "n" in gr ")" /*
- */ _col(50) _request(answ)
- }
- end
-
-
- _yesno "Would you like to see examples of each"
- if lower("%answ")=="y" {
- set more 0
- capture noisily graph using %path`twoway
- %_pause
- capture noisily graph using %path`matrix
- %_pause
- capture noisily graph using %path`hist
- %_pause
- capture noisily graph using %path`oneway
- %_pause
- capture noisily graph using %path`box
- %_pause
- capture noisily graph using %path`star
- %_pause
- capture noisily graph using %path`bar
- %_pause
- capture noisily graph using %path`pie
- set more 1
- _yesno "Would you like to see them again"
- if lower("%answ")=="y" {
- set more 0
- capture noisily graph using %path`twoway
- %_pause
- capture noisily graph using %path`matrix
- %_pause
- capture noisily graph using %path`hist
- %_pause
- capture noisily graph using %path`oneway
- %_pause
- capture noisily graph using %path`box
- %_pause
- capture noisily graph using %path`star
- %_pause
- capture noisily graph using %path`bar
- %_pause
- capture noisily graph using %path`pie
- set more 1
- capture }
- capture }
-
- capture program drop _yesno
- macro def answ
-
- #delimit ;
-
- di _n(9) in wh
- "Enough preliminary" _n
- "------------------" _n ;
-
- di _n in wh _dup(79) "-" _n in gr
- "To teach you how to use Stata's graphics capabilities, we will use the auto-"
- _n
- "mobile data that we used in the introduction. Remember that this data is" _n
- "stored in the file 'auto.dta':" _n
- in wh _dup(79) "-" _n(2)
- ". use %path`auto, clear" ;
- noisily use %path`auto, clear ;
-
-
- di _n(6) in wh _dup(79) "-" _n in gr
- "We will begin with the two-way scatterplot, plotting miles per gallon against"
- _n
- "engine displacement:" _n
- in wh _dup(79) "-" _n(2)
- ". graph mpg displ" ;
- set more 0 ; more ;
- capture noisily graph mpg displ ;
- set more 1 ;
-
- di _n(12) in wh _dup(79) "-" _n in gr
- "Stata will allow us to manipulate the basic image and add labeling, grid" _n
- "lines, and titles. Let's ask Stata to label the axes:" _n
- in wh _dup(79) "-" _n(2)
- ". graph mpg displ, xlabel ylabel" ;
- set more 0 ; more ;
- capture noisily graph mpg displ, xlabel ylabel ;
- set more 1 ;
-
- di _n(7) in wh _dup(79) "-" _n in gr
- "We'll get to grid lines and titles in a moment, but first we'll show you"
- _n
- "how to vary the text size. The Stata parameter "
- in wh "textsize" in gr " says how big the" _n
- "characters are supposed to be. Its default value is 100, which means 100%" _n
- "of what Stata thinks looks good. Let's redraw our last image varying the" _n
- in wh "textsize" in gr ":" _n
- in wh _dup(79) "-" _n(2)
- ". set textsize 200" _n(2)
- ". graph mpg displ, xlabel ylabel" ;
- set more 0 ; more ;
- set textsize 200 ;
- capture noisily graph mpg displ, xlabel ylabel gap(3);
- set more 1 ;
-
- di in wh _n(3)
- ". set textsize 125" _n(2)
- ". graph mpg displ, xlabel ylabel" ;
- set more 0 ; more ;
- set textsize 125 ;
- capture noisily graph mpg displ, xlabel ylabel gap(4);
- set more 1 ;
-
- di in wh _n(3)
- ". set textsize 75" _n(2)
- ". graph mpg displ, xlabel ylabel" ;
- set more 0 ; more ;
- set textsize 75 ;
- capture noisily graph mpg displ, xlabel ylabel gap(4);
- set more 1 ;
-
- di _n(17) in wh _dup(79) "-" _n in gr
- "You get the idea. We're not going to play with "
- in wh "textsize" in gr " again, but you may" _n
- "want to. We recommend that PC users with the Color Graphics Adapter '"
- in wh "set" _n
- "textsize 125" in gr "'. We'll set it back to the default 100:" _n
- in wh _dup(79) "-" _n(2)
- ". set textsize 100" ;
- set textsize 100 ;
- set more 0 ; more ; set more 1 ;
-
-
- di _n(7) in wh _dup(79) "-" _n in gr
- "Now let's return to adding titles and grid lines. There are two titles on" _n
- "every side of the graph, called "
- in wh "t1title" in gr ", " in wh "t2title" in gr ", "
- in wh "b1title" in gr ", " in wh "b2title" in gr ", "
- in wh "l1title" in gr "," _n
- in wh "l2title" in gr ", "
- in wh "r1title" in gr ", and " in wh "r2title"
- in wh ". The '" in wh "t" in gr "', '"
- in wh "b" in gr "', '" in wh "l" in gr "', and '" in wh "r"
- in gr "' stand for top," _n
- "bottom, left, and right. The "
- in wh "b1title" in gr " is also known as simply the "
- in wh "title" in gr "." _n ;
-
- di in gr
- "Here's a graph showing where all the titles are:" _n
- in wh _dup(79) "-" _n(2)
- ". graph mpg displ, t1title(This is the t1title)" _n
- " t2title(This is the t2title)" _n
- " b1title(This is the b1title also known as title)" _n
- " b2title(This is the b2title)" _n
- " r1title(This is the r1title)" _n
- " r2title(This is the r2title)" _n
- " l1title(This is the l1title)" _n
- " l2title(This is the l2title)" ;
- set more 0 ; more ;
- capture noisily gr mpg displ, gap(4)
- t1(This is the t1title) t2(This is the t2title)
- b1(" This is the b1title also known as title")
- b2(This is the b2title)
- r1(This is the r1title) r2(This is the r2title)
- l1(This is the l1title) l2(This is the l2title) ;
- set more 1 ;
-
- di _n(13) in wh _dup(79) "-" _n in gr
- "In practice, no one types out words like "
- in wh "l1title" in gr " and " in wh "b2title" in gr ". The abbre-" _n
- "viations are "
- in wh "l1" in gr ", "
- in wh "l2" in gr ", "
- in wh "t1" in gr ", "
- in wh "t2" in gr ", "
- in wh "b1" in gr ", "
- in wh "b2" in gr ", "
- in wh "r1" in gr ", and "
- in wh "r2" in gr ". The more easily remem-" _n
- "bered word " in wh "title"
- in gr " is a synonym for " in wh "b1title"
- in gr ", and it can be abbreviated as" _n
- in wh "ti" in gr "." _n ;
-
- di in gr
- "So now, let's go back and title our graph:" _n
- in wh _dup(79) "-" _n(2)
- ". graph mpg displ, xlabel ylabel ti(MPG vs. Engine Displacement)" _n
- " t1(1978 Automobile Data)" ;
- set more 0 ; more ;
- capture noisily graph mpg displ, xlabel ylabel gap(4)
- title(" MPG vs. Engine Displacement")
- t1(1978 Automobile Data) ;
- set more 1 ;
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "You might prefer:" _n
- in wh _dup(79) "-" _n(2)
- ". graph mpg displ, xlabel ylabel ti(MPG vs. Engine Displacement)" _n
- " t1(1978 Automobile Data) border" ;
- set more 0 ; more ;
- capture noisily graph mpg displ, xlabel ylabel gap(4)
- title(" MPG vs. Engine Displacement")
- t1(1978 Automobile Data) border t2(" ") ;
- set more 1 ;
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "Or even:" _n
- in wh _dup(79) "-" _n(2)
- ". graph mpg displ, xlabel ylabel ti(MPG vs. Engine Displacement)" _n
- " t1(1978 Automobile Data) border tlabel rlabel" ;
- set more 0 ; more ;
- capture noisily graph mpg displ, xlabel ylabel gap(4)
- title(" MPG vs. Engine Displacement")
- t1(1978 Automobile Data) border t2(" ") tlab rlab;
- set more 1 ;
-
-
-
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "We can also ask Stata to overlay a grid by suppressing the axes and drawing" _n
- "lines across the graph. In the case of the y-axis, we tell Stata to label" _n
- "and line where it thinks best. In the case of the x-axis, we tell Stata" _n
- "exactly where we want the lines and labels:" _n
- in wh _dup(79) "-" _n(2)
- ". graph mpg displ, ylabel yline t1(1978 Automobile Data)" _n
- " xlabel(75,175,275,375,475) xline(75,175,275,375,475)" _n
- " noaxis ti(MPG vs. Engine Displacement)";
- set more 0 ; more ;
- capture noisily graph mpg displ, xlabel(75,175,275,375,475) ylabel
- xline(75,175,275,375,475) yline noaxis
- ti(" MPG vs. Engine Displacement")
- t1(1978 Automobile Data) gap(4) ;
- set more 1 ;
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "Perhaps we should label the right and top of the image, too:" _n
- in wh _dup(79) "-" _n(2)
- ". graph mpg displ, ylabel yline t1(1978 Automobile Data)" _n
- " xlabel(75,175,275,375,475) xline(75,175,275,375,475)" _n
- " tlabel(75,175,275,375,475) rlabel" _n
- " noaxis ti(MPG vs. Engine Displacement)";
- set more 0 ; more ;
- capture noisily graph mpg displ, xlabel(75,175,275,375,475) ylabel rlabel
- xline(75,175,275,375,475) yline noaxis
- tlabe(75,175,275,375,475)
- ti("MPG vs. Engine Displacement")
- t1(1978 Automobile Data) gap(4) ;
- set more 1 ;
-
-
- di _n(18) in wh _dup(79) "-" _n in gr
- "Let's go back to the simple image, but this time ask Stata to connect the" _n
- "points with straight lines. Our first example will not be what we want:" _n
- in wh _dup(79) "-" _n(2)
- ". graph mpg displ, connect(l)" ;
- set more 0 ; more ;
- capture noisily graph mpg displ, c(l) gap(4)
- t1("(This is a mess because Stata connected the points in")
- t2("the order of the data!)") ;
- set more 1 ;
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "That didn't work out so well. We can tell Stata to connect the points in" _n
- "the order of the x-axis by adding the '"
- in wh "sort" in gr "' option. We will also begin" _n
- "abbreviating our commands and options because Stata users seldom type out" _n
- "the words:" _n
- in wh _dup(79) "-" _n(2)
- ". gr mpg displ, c(l) sort" ;
- set more 0 ; more ;
- capture noisily graph mpg displ, c(l) sort gap(4);
- set more 1 ;
-
- di _n(6) in wh _dup(79) "-" _n in gr
- "That was better, but connecting the points was still not very informative." _n
- "Stata can connect points using a smooth cubic spline. It does this by di-" _n
- "viding the x-axis into 200 intervals and calculating the median x and y" _n
- "value in each. Those values are then used for the calculation and drawing" _n
- "of a cubic spline. We get this by saying "
- in wh "connect(s)" in gr " rather than " in wh "connect(l)" in gr "." _n
- in wh _dup(79) "-" _n(2)
- ". gr mpg displ, c(s)" ;
- set more 0 ; more ;
- capture noisily graph mpg displ, c(s) gap(4)
- t1("(Oops! It's oscillating. We can fix that.)") ;
- set more 1 ;
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "We can use splines to achieve data smoothing by reducing the number of bands"
- _n
- "Stata uses to fit the spline function. By default, Stata uses 200 bands." _n
- "Let's try just 8." _n
- in wh _dup(79) "-" _n(2)
- ". gr mpg displ, c(s) bands(8)" ;
- set more 0 ; more ;
- capture noisily graph mpg displ, c(s) bands(8) gap(4);
- set more 1 ;
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "Splines are only one way of graphically summarizing data. We could, for" _n
- "instance, draw the regression line. To do this we must run the regression,"
- _n
- "retrieve the prediction, and then make the graph:" _n
- in wh _dup(79) "-" _n(2)
- ". regress mpg displ" ;
- capture noisily regress mpg displ ;
- set more 0 ; more ; set more 1 ;
- di _n(3) in wh ". predict hat" ;
- capture noisily predict hat ;
- di _n in wh ". gr mpg hat displ, symbol(Oi) c(.l) sort" ;
- set more 0 ; more ;
- capture noisily gr mpg hat displ, symbol(Oi) c(.l) sort ;
- set more 1 ;
-
- di _n(8) in wh _dup(79) "-" _n in gr
- "We played some games in the last graph with the "
- in wh "symbol" in gr " and " in wh "connect" in gr " options." _n
- "You can learn how they work by typing '"
- in wh "help graph" in gr "' at the conclusion of this"
- _n
- "tutorial." _n(2)
- "Since we know what we're doing, we can even combine the data, the spline," _n
- "and the regression line in a single image:" _n
- in wh _dup(79) "-" _n(2)
- ". gr mpg hat displ, s(Oi) c(sl) sort bands(8)" ;
- set more 0 ; more ;
- capture noisily gr mpg hat displ, s(Oi) c(sl) sort bands(8) gap(4) ;
- set more 1 ;
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "You probably suspect that Stata has different point styles -- circles, tri-" _n
- "angles, squares, and the like -- and you're right. More importantly, Stata" _n
- "also has the unique ability to use variables in your data as the plotting" _n
- "symbol. For instance, in our data we have a variable called make recording" _n
- "the make of the car:" _n
- in wh _dup(79) "-" _n(2)
- ". list make in 1/5" ;
- noisily list make in 1/5 ;
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "We can use this variable as our plotting symbol:" _n
- in wh _dup(79) "-" _n(2)
- ". graph mpg displ, symbol([make])" ;
- set more 0 ; more ;
- capture noisily graph mpg displ, s([make]) gap(4)
- t1("(You wouldn't want to publish this graph, but it")
- t2("is useful for analysis.)") ;
- set more 1 ;
-
- drop gratio length trunk hdroom ;
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "There was too much data to make a presentation quality graph. You might" _n
- "also have noticed that the words were trimmed to 8 characters. You can" _n
- "increase that, but it would only increase the amount of overprinting." _n(2)
- "Let's restrict ourselves to the foreign cars, increase the point size to" _n
- "125% of what Stata normally uses, and trim the words at 32 characters:" _n
- in wh _dup(79) "-" _n(2)
- ". graph mpg displ if foreign, s([make]) psize(125) trim(32)" ;
- set more 0 ; more ;
- capture noisily graph mpg displ if foreign, s([make]) ps(125) tr(32) gap(4)
- xsca(.,170)
- t1("(We still have overprinting)") ;
- set more 1 ;
-
- di _n(7) in wh _dup(79) "-" _n in gr
- "If we were willing to go to some work, we could use this capability to draw"
- _n
- "a truly presentable graph. Remember that we ran a regression of mpg on" _n
- "displ. We could use Stata's standard circle symbol for most of the data" _n
- "and the text symbols for the outliers. To do that, first we need the re-" _n
- "siduals from our previous regression:" _n
- in wh _dup(79) "-" _n(2)
- ". predict res, residuals" ;
- noisily predict res, residuals ;
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "Next, we need to examine those residuals to find out what an outlier is:" _n
- in wh _dup(79) "-" _n(2)
- ". summarize res, detail" ;
- set more 0 ; more ; set more 1 ;
- noisily summarize res, detail ;
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "We'll call an outlier anything below the 5th or above the 95th percentile." _n
- "We see that the corresponding numbers are -6.11 and 9.2:" _n
- in wh _dup(79) "-" _n(2)
- ". generate outlier = mpg if res<-6.11 | res>9.2" ;
- set more 0 ; more ; set more 1 ;
- noisily gen outlier = mpg if res<-6.11 | res>9.2 ;
-
- di _n in wh ". gen goodpt = mpg if outlier==." ;
- noisily gen goodpt = mpg if outlier==. ;
-
- replace outlier=. in 69 ;
- replace good=. in 57 ;
-
- di _n in wh
- ". graph goodpt outlier hat displ, s(O[make]i) c(..l) sort" _n
- " l1(Mileage) t1(Outliers named) psize(115) trim(32)" ;
- set more 0 ; more ;
- capture noisily graph goodpt outlier hat displ,
- s(O[make]i) c(..l) sort
- l1("Mileage") t1(Outliers named) psize(115) trim(32) gap(4) ;
- set more 1;
-
- di _n(15) in wh _dup(79) "-" _n in gr
- "You probably think that, by now, you know everything there is to know about" _n
- "the two-way scatterplot. You're wrong. Stata has the ability to draw dif-" _n
- "ferent scatterplots in the same image for groups of the data. Our data con-"
- _n
- "tains both foreign and domestic cars. Let's draw separate graphs for foreign"
- _n
- "cars, domestic cars, and both groups combined:" _n
- in wh _dup(79) "-" _n(2)
- ". graph mpg displ, ylabel xlabel by(foreign) total" ;
- set more 0 ; more ;
- capture noisily graph mpg displ, ylabel xlabel by(foreign) total ;
- set more 1 ;
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "We'll come back to this feature later. It is not limited to only two groups;"
- _n
- "in fact, Stata can put up to 400 graphs on a single screen!" _n
- in wh _dup(79) "-" _n(2) ;
-
- use %path`auto, clear ;
- set more 0 ; more ; set more 1 ;
-
- di _n(16) in wh _dup(79) "-" _n in gr
- "One of Stata's more powerful images is the scatterplot matrix, a generaliza-"
- _n
- "tion of the scatterplot. Making them is easy:" _n
- in wh _dup(79) "-" _n(2)
- ". graph mpg displ weight gratio, matrix label" ;
- set more 0 ; more ;
- gr mpg displ weight gratio, mat label
- t1("On the first row are graphs of mileage against displ, weight,")
- t2("and gratio")
- l1("On the second row are graphs of displ")
- l2("against mileage, weight, and gratio")
- r2("On the third row are graphs of weight")
- r1("against mileage, displ, and gratio")
- b2("On the fourth row are graphs of gratio against mileage, displ, weight") ;
- set more 1 ;
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "Stata views scatterplot matrices as a generalization of the standard two-way"
- _n
- "scatterplot, so anything we can do to a two-way scatterplot we can do to a" _n
- "scatterplot matrix." _n(2)
- "In the next example, we redraw the scatterplot matrix and include a smoothed"
- _n
- "spline function using 8 bands:" _n
- in wh _dup(79) "-" _n(2)
- ". graph mpg displ weight gratio, matrix label c(s) bands(8)" ;
- set more 0 ; more ;
- capture noisily graph mpg displ weight gratio, matrix label c(s) bands(8) ;
- set more 1 ;
-
- di _n(3) in wh _dup(79) "-" _n in gr
- "So far we have drawn 4-by-4 scatterplots. You may wonder how far we can go."
- _n
- "Stata will draw 30-by-30 scatterplots, which are so small as to be unreadable."
- _n
- "Here is an 8-by-8 matrix:" _n
- in wh _dup(79) "-" _n(2)
- ". graph price mpg weight displ hdroom trunk length turn, matrix label sy(.)" ;
- set more 0 ; more ;
- capture noisily gr price mpg weight displ hdroom trunk length turn,
- matrix label sy(.);
- set more 1 ;
-
-
- di _n(18) in wh _dup(79) "-" _n in gr
- "Stata can draw histograms. Below we draw a histogram using 7 bins with an" _n
- "overlaid normal:" _n
- in wh _dup(79) "-" _n(2)
- ". graph mpg, bin(7) ylabel xlabel normal" ;
- set more 0 ; more ;
- capture noisily graph mpg, bin(7) ylabel xlabel normal gap(4) ;
- set more 1 ;
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "We can combine the histogram style with the "
- in wh "by()" in gr " option to obtain separate" _n
- "histograms by car type:" _n
- in wh _dup(79) "-" _n(2)
- ". graph mpg, bin(7) ylabel xlabel normal by(foreign) total" ;
- set more 0 ; more ;
- capture noisily graph mpg, bin(7) ylabel xlabel normal by(foreign) total ;
- set more 1 ;
-
-
-
- di _n(14) in wh _dup(79) "-" _n in gr
- "In addition to histograms, Stata provides the one-way scatterplot. One-way" _n
- "scatterplots show the distribution of the data by drawing an axis and then" _n
- "placing vertical lines everywhere that data occurs." _n(2)
- "Here's an example of a one-way scatterplot of price:" _n
- in wh _dup(79) "-" _n(2)
- ". graph price, oneway by(foreign) total ti(Distribution of Price)" ;
- set more 0 ; more ;
- capture noisily graph price, oneway by(foreign) total
- ti("Distribution of Price") ;
- set more 1 ;
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "A box plot shows the distribution of the data by drawing a box showing the" _n
- "median, 25th and 75th percentiles, and the upper- and lower-adjacent values."
- _n
- "Points outside the adjacent values, called outside values, are plotted indi-"
- _n
- "vidually." _n
- in wh _dup(79) "-" _n(2)
- ". graph price, box by(foreign) total ylabel" ;
- set more 0 ; more ;
- capture noisily graph price, box by(foreign) total ylabel
- ti("Comparison of Domestic and Foreign Cars") ;
- set more 1 ;
-
- di _n(3) in wh _dup(79) "-" _n in gr
- "Stata can combine the box and one-way methods of showing a distribution," _n
- "resulting in a more informative display:" _n
- in wh _dup(79) "-" _n(2)
- ". graph price, oneway box by(foreign) total" ;
- set more 0 ; more ;
- capture noisily graph price, oneway box by(foreign) total
- ti("Comparison of Domestic and Foreign Cars") ;
- set more 1 ;
-
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "Stata even allows you to combine two-way scatterplots, one-way scatterplots,"
- _n
- "and box plots into a single, informative diagram:" _n
- in wh _dup(79) "-" _n(2)
- ". graph price weight, twoway oneway box" ;
- set more 0 ; more ;
- capture noisily graph price weight, twoway oneway box gap(6)
- t1("(We call this our kitchen-sink diagram.)")
- t2(" ") ;
- set more 1 ;
- di in wh _n
- ". graph price weight, twoway oneway box by(foreign) total" ;
- set more 0 ; more ;
- capture noisily graph price weight, twoway oneway box by(foreign) total ;
- set more 1 ;
-
- replace make="Renault" in 65 ;
-
- di _n(20) in wh _dup(79) "-" _n in gr
- "Stata can draw star charts:" _n
- in wh _dup(79) "-" _n(2)
- ". graph price-displ if foreign, star label(make)" ;
- set more 0 ; more ;
- capture noisily graph price-displ if foreign, star label(make) bsize(130) ;
- set more 1 ;
-
- drop _all ;
- label drop _all ;
- input str4 year labor parts advert overhead profits dlabor ilabor ;
- FY82 1000 500 100 20 -900 800 200 ;
- FY83 2000 800 200 40 -400 1600 400 ;
- FY84 2200 900 300 50 -200 1760 440 ;
- FY85 2400 1000 400 100 40 1920 480 ;
- FY86 2400 1000 400 60 100 1920 480 ;
- FY87 2800 1200 500 110 210 2240 560 ;
- FY88 2900 1500 550 120 450 2320 580 ;
- end ;
- label var year "Fiscal Year" ;
- label var labor "Labor costs" ;
- label var parts "Raw materials" ;
- label var advert "Advertising" ;
- label var overhead "Overhead" ;
- label var profits "Profits" ;
- label var dlabor "Direct labor" ;
- label var ilabor "Indirect labor" ;
- format year %4s ;
- format labor parts advert dlabor ilabor %6.0g ;
- format overhead %8.0g ;
- format profits %7.0g ;
-
- di _n(8) in wh _dup(79) "-" _n in gr
- "To demonstrate Stata's bar and pie chart capabilities, we will use data on" _n
- "costs and revenues for a hypothetical firm. We have data for FY82 to FY88."
- _n
- "We've already loaded the data:" _n
- in wh _dup(79) "-" _n(2)
- ". list" ;
- noisily list, nodisplay ;
-
- di _n(2) in wh _dup(79) "-" _n in gr
- "Let's begin by drawing a bar chart:" _n
- in wh _dup(79) "-" _n(2)
- ". graph labor parts overhead profits, bar by(year) ylabel yline noaxis" ;
- set more 0 ; more ;
- capture noisily graph labor parts overhead profits, bar by(year) ylabel yline
- noaxis pen(2354) ;
- set more 1 ;
-
-
- di _n(13) in wh _dup(79) "-" _n in gr
- "Perhaps you prefer stacked bar charts:" _n
- in wh _dup(79) "-" _n(2)
- ". graph labor parts overhead profits, bar by(year) ylabel yline noaxis stack" ;
- set more 0 ; more ;
- capture noisily graph labor parts overhead profits, bar by(year) ylabel yline
- noaxis pen(2354) stack ;
- set more 1 ;
-
- di _n(13) in wh _dup(79) "-" _n in gr
- "Pie charts are equally easy:" _n
- in wh _dup(79) "-" _n(2)
- ". graph dlabor ilabor parts advert overhead, pie by(year)" ;
- set more 0 ; more ;
- capture noisily graph dlabor ilabor parts advert overhead, pie by(year) ;
- set more 1 ;
-
- di _n(18) in wh _dup(79) "-" _n in gr
- "Stata has one more capability: the ability to combine graphs. At the be-" _n
- "ginning of this tutorial, we showed you examples of each of Stata's styles." _n
- "Each of those examples is saved in a disk file and was created using "
- in wh "graph" in gr "'s"_n
- in wh "saving" in gr " option." _n(2)
- "The files were named twoway.gph, matrix.gph, hist.gph, and so on. We can" _n
- "tell Stata to combine the graphs into a single image with the '"
- in wh "graph using" in gr "'" _n
- "command:" _n
- in wh _dup(79) "-" _n ;
-
- di in wh
- ". graph using %path`twoway %path`matrix" _n
- " %path`hist %path`oneway, margin(10)" _n
- " title(Summary of Temperature Data)" ;
- set more 0 ; more ;
- capture noisily graph using %path`twoway %path`matrix
- %path`hist %path`oneway, mar(10)
- title(Summary of Temperature Data) ;
- set more 1 ;
-
- di _n(18) in wh
- "Find out more" _n
- "-------------" _n ;
-
- di in gr
- "You can learn more about the "
- in wh "graph" in gr " command by typing '"
- in wh "help graph" in gr "'." _n(2)
- "If you are interested in reading more about the kinds of images presented" _n
- "here, we highly recommend two books:" _n ;
-
- di in wh _col(10)
- "Graphical Methods for Data Analysis" in gr ", by John Chambers," _n
- _col(10) "William Kleiner, and Paul A. Tukey, The Wadsworth" _n
- _col(10) "Statistics/Probability Series" _n ;
-
- di in wh _col(10)
- "The Visual Display of Quantitative Information" in gr ", by Edward" _n
- _col(10) "R. Tufte, Graphics Press, Chesire, Connecticut" _n(10) ;
- set more 0 ; more ; set more 1 ;
-
-
- di _n(4) in white
- "Demonstration ends" _n
- "------------------" _n ;
-
- macro define F6 "do %path`tables.tut;" ;
-
- di in green
- "That concludes our short demonstration, but there's much more. We now return"
- _n
- "control to you. Some suggestions:" _n ;
-
- di in green
- "If you ..." _col(34) "Then we will show you ..." _n
- " Press " in white "F5" in green _col(38) "a table of tutorial contents" _n
- " Press " in white "F6" in green _col(38) "the next tutorial, "
- in white "tables.tut" _n ;
-
- run %path`tobuy.tut ;